import 'dart:math';

import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:loading_indicator/loading_indicator.dart';
import 'package:gold/utils/size_extension.dart';
import 'package:gold/utils/xfg_utils.dart';

/// 描述: 先锋拓展
/// 
/// @Author tangzhentao
///  @CreatedTime 19:56 2022/6/17
///

/// 字符串扩展
extension StringExtension on String {
  toast({
    Duration? duration = const Duration(milliseconds: 1500),
    EasyLoadingToastPosition? position = EasyLoadingToastPosition.center,
    EasyLoadingMaskType? maskType = EasyLoadingMaskType.clear
  }) => EasyLoading.showToast(this, duration: duration, toastPosition: position, maskType: maskType, dismissOnTap: false);

  info({
    Duration? duration = const Duration(milliseconds: 1500),
    EasyLoadingToastPosition? position = EasyLoadingToastPosition.center,
    EasyLoadingMaskType? maskType = EasyLoadingMaskType.clear
  }) => EasyLoading.showInfo(this, duration: duration, maskType: maskType, dismissOnTap: false);

  error({
    Duration? duration = const Duration(milliseconds: 1500),
    EasyLoadingMaskType? maskType = EasyLoadingMaskType.clear
  }) => EasyLoading.showError(this, duration: duration, maskType: maskType, dismissOnTap: false);

  success({
    Duration? duration = const Duration(milliseconds: 1500),
    EasyLoadingMaskType? maskType = EasyLoadingMaskType.clear
  }) => EasyLoading.showSuccess(this, duration: duration, maskType: maskType, dismissOnTap: false);

  loading({
    Widget? indicator,
    EasyLoadingMaskType? maskType = EasyLoadingMaskType.clear
  }) => EasyLoading.show(status: this, indicator: indicator ?? SizedBox(
    height: 32.xr,
    child: const LoadingIndicator(colors: [Colors.white], indicatorType: Indicator.ballPulse,),
  ), maskType: maskType, dismissOnTap: false);

  /// 16进制颜色值转为Color
  Color? hexToColor() {
    String hexColor = toUpperCase().replaceAll("#", '');
    xfgPrint.call('颜色1 $hexColor');
    if (hexColor.length == 6 || hexColor.length == 8) {
      String newHexColor = '0x${hexColor.length == 6 ? 'FF':''}$hexColor';
      xfgPrint.call('颜色2 $newHexColor');
      final color = int.tryParse(newHexColor);
      if (color == null) return null;
      return Color(color);
    }
    return null;
  }
}

extension NumExtension on num {

  /// 数字转换为时分秒
  String secondToHourMinuteSecond() {
    final hour = this ~/ 3600;
    final minute = (this % 3600) ~/ 60;
    final second = this - hour * 3600 - minute * 60;
    return '${hour < 10 ? '0$hour':'$hour'}:${minute < 10 ? '0$minute':'$minute'}:${second < 10 ? '0$second':'$second'}';
  }

  /// 数字转换为分秒
  String secondToMinuteSecond() {
    final minute = this ~/ 60;
    final second = this % 60;
    return '${minute < 10 ? '0$minute':'$minute'}:${second < 10 ? '0$second':'$second'}';
  }

  /// 弧度转为角度值
  double radianToAngle() {
    assert(this >= 0.0 && this <= 1.0);
    return this * 180 / (pi);
  }

  /// 角度值转为弧度
  double angleToRadian() {
    assert(this >= 0 && this <= 360);
    return this * pi / 180;
  }
}

const formatYYYYMMDDChinese = [yyyy, '年', mm, '月', dd, '日'];
const formatYYYYMMChinese = [yyyy, '年', mm, '月'];
const formatYYYYChinese = [yyyy, '年'];
const formatMMDDHHNNChinese = [mm, '月', dd, '日', ' ', HH, ':', nn];

extension DateExtension on DateTime? {
  String format({List<String> formats = const [yyyy, '-', mm, '-', dd], String nullString = '请输入日期'}) {
    if(this != null) {
      return formatDate(this!, formats);
    } else {
      return nullString;
    }
  }
}